﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class ServerModels
    {
           public List<Server> GetServer()
        {
            ConnectDB con=new ConnectDB();
            List<Server>listverver=new List<Server>();
            try
            {
                string sql = "Select ServerID,ServerName,Desciption,GameName from TblServer join TblGame on TblServer.GameID=TblGame.GameID";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Server sv = new Server();
                    sv.ServerID =int.Parse(con.dataRead["ServerID"].ToString());
                    sv.ServerName = con.dataRead["ServerName"].ToString();
                    sv.Desciption = con.dataRead["Desciption"].ToString();
                    sv.GameName=con.dataRead["GameName"].ToString();
                    listverver.Add(sv);

                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
            return listverver;
        }
           public void CreateServer(Server sv)
           {
               ConnectDB con = new ConnectDB();
               try
               {
                   string sql = "sp_CreateServer @ServerName=N'" + sv.ServerName + "', @Desciption=N'" + sv.Desciption + "', @GameID='" + sv.GameID + "' ";
                   con.ExeNonQuery(sql);
               }
               catch (Exception ex)
               {
                   Console.Write(ex);
               }
               finally { con.CloseData(); con = null; }
           }
           public void EditServer(Server sv)
           {
               ConnectDB con = new ConnectDB();
               try
               {
                   string sql = "sp_EditServer  @ServerID='"+sv.ServerID+"' ,@ServerName=N'"+sv.ServerName+"', @Desciption=N'"+sv.Desciption+"', @GameID='"+sv.GameID+"'";
                   con.ExeNonQuery(sql);
               }
               catch (Exception ex)
               {
                   Console.Write(ex);
               }
               finally { con.CloseData(); con = null; }
           }
           public void DeleteServer(int ServerID)
           {
               ConnectDB con = new ConnectDB();
               try
               {
                   string sql = "DeleteServer @ServerID='" + ServerID + "'";
                   con.ExeNonQuery(sql);
               }
               catch (Exception ex)
               {
                   Console.Write(ex);
               }
               finally { con.CloseData(); con = null; }
           }
           public List<Server> GetServerID(int ServerID)
           {
               ConnectDB con = new ConnectDB();
               List<Server> listververid = new List<Server>();
               try
               {
                   string sql = "sp_GetServerID @ServerID='"+ServerID+"'";
                   con.ExeReader(sql);
                   while (con.dataRead.Read())
                   {
                       Server sv = new Server();
                       sv.ServerID = int.Parse(con.dataRead["ServerID"].ToString());
                       sv.ServerName = con.dataRead["ServerName"].ToString();
                       sv.Desciption = con.dataRead["Desciption"].ToString();
                       sv.GameID = int.Parse(con.dataRead["GameID"].ToString());
                       listververid.Add(sv);

                   }
               }
               catch (Exception ex)
               {
                   Console.Write(ex);
               }
               finally { con.CloseData(); con = null; }
               return listververid;
           }

    }
}